// 获取用户的基本信息
$(function () {
    get()
    //给退出按钮绑定点击事件 设置layui弹出层

    var layer = layui.layer
    $('#btnlogit').on('click', function () {
        layer.confirm('确定退出?', { icon: 3, title: '提示' }, function (index) {
            //1.清空本地存储
            localStorage.removeItem('token')
            // 2.跳转登陆页面
            location.href = '../../login.html'
            layer.close(index);
        });
    })
})
function get() {
    $.ajax({
        method: 'GET',
        url: "/my/userinfo",
        /*    headers: {
               Authorization: localStorage.getItem('token') || ''
           }, */
        success: function (res) {
            if (res.code !== 0) {
                return layui.layer.msg('获取用户休息失败')
            }
            //调用render函数渲染用户头像
            render(res.data)
        },
        /*   complete: function (res) {
              console.log(res);
              if (res.responseJSON.code === 1 && res.responseJSON.message === "身份认证失败！") {
                  //  1.清空本地存储
                  localStorage.removeItem('token')
                  // 2.跳转登录页面
                  location.href = '../../login.html'
              }
          } */

    })
}
function render(user) {
    console.log(user);
    var name = user.nickname || user.username
    $('#welcome').html('欢迎&nbsp;&nbsp;' + name)
    if (user.user_pic !== null) {
        $('.layui-nav-img').attr('src', user.user_pic).show()
        $('.text-avatar').hide()
    } else {
        $('.layui-nav-img').hide()
        var fide = name[0].toUpperCase()
        $('.text-avatar').html(fide).show()
    }
}